<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::CoreExtensions::Array::Grouping</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::CoreExtensions::Array::Grouping</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/vendor/rails/activesupport/lib/active_support/core_ext/array/grouping_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/array/grouping.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000493">in_groups</a>&nbsp;&nbsp;
      <a href="#M000492">in_groups_of</a>&nbsp;&nbsp;
      <a href="#M000494">split</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000493" class="method-detail">
        <a name="M000493"></a>

        <div class="method-heading">
          <a href="#M000493" class="method-signature">
          <span class="method-name">in_groups</span><span class="method-args">(number, fill_with = nil) {|g| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Splits or iterates over the array in <tt>number</tt> of groups, padding any
remaining slots with <tt>fill_with</tt> unless it is <tt>false</tt>.
</p>
<pre>
  %w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|group| p group}
  [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]
  [&quot;5&quot;, &quot;6&quot;, &quot;7&quot;, nil]
  [&quot;8&quot;, &quot;9&quot;, &quot;10&quot;, nil]

  %w(1 2 3 4 5 6 7).in_groups(3, '&amp;nbsp;') {|group| p group}
  [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]
  [&quot;4&quot;, &quot;5&quot;, &quot;&amp;nbsp;&quot;]
  [&quot;6&quot;, &quot;7&quot;, &quot;&amp;nbsp;&quot;]

  %w(1 2 3 4 5 6 7).in_groups(3, false) {|group| p group}
  [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]
  [&quot;4&quot;, &quot;5&quot;]
  [&quot;6&quot;, &quot;7&quot;]
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000493-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000493-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/grouping.rb, line 59</span>
59:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">in_groups</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">fill_with</span> = <span class="ruby-keyword kw">nil</span>)
60:           <span class="ruby-comment cmt"># size / number gives minor group size;</span>
61:           <span class="ruby-comment cmt"># size % number gives how many objects need extra accomodation;</span>
62:           <span class="ruby-comment cmt"># each group hold either division or division + 1 items.</span>
63:           <span class="ruby-identifier">division</span> = <span class="ruby-identifier">size</span> <span class="ruby-operator">/</span> <span class="ruby-identifier">number</span>
64:           <span class="ruby-identifier">modulo</span> = <span class="ruby-identifier">size</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">number</span>
65: 
66:           <span class="ruby-comment cmt"># create a new array avoiding dup</span>
67:           <span class="ruby-identifier">groups</span> = []
68:           <span class="ruby-identifier">start</span> = <span class="ruby-value">0</span>
69: 
70:           <span class="ruby-identifier">number</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
71:             <span class="ruby-identifier">length</span> = <span class="ruby-identifier">division</span> <span class="ruby-operator">+</span> (<span class="ruby-identifier">modulo</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">modulo</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">index</span> <span class="ruby-value">? </span><span class="ruby-value">1</span> <span class="ruby-operator">:</span> <span class="ruby-value">0</span>)
72:             <span class="ruby-identifier">padding</span> = <span class="ruby-identifier">fill_with</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">false</span> <span class="ruby-operator">&amp;&amp;</span>
73:               <span class="ruby-identifier">modulo</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">division</span> <span class="ruby-value">? </span><span class="ruby-value">1</span> <span class="ruby-operator">:</span> <span class="ruby-value">0</span>
74:             <span class="ruby-identifier">groups</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">slice</span>(<span class="ruby-identifier">start</span>, <span class="ruby-identifier">length</span>).<span class="ruby-identifier">concat</span>([<span class="ruby-identifier">fill_with</span>] <span class="ruby-operator">*</span> <span class="ruby-identifier">padding</span>)
75:             <span class="ruby-identifier">start</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">length</span>
76:           <span class="ruby-keyword kw">end</span>
77: 
78:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
79:             <span class="ruby-identifier">groups</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">g</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">g</span>) }
80:           <span class="ruby-keyword kw">else</span>
81:             <span class="ruby-identifier">groups</span>
82:           <span class="ruby-keyword kw">end</span>
83:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000492" class="method-detail">
        <a name="M000492"></a>

        <div class="method-heading">
          <a href="#M000492" class="method-signature">
          <span class="method-name">in_groups_of</span><span class="method-args">(number, fill_with = nil) {|slice| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Splits or iterates over the array in groups of size <tt>number</tt>,
padding any remaining slots with <tt>fill_with</tt> unless it is
<tt>false</tt>.
</p>
<pre>
  %w(1 2 3 4 5 6 7).in_groups_of(3) {|group| p group}
  [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]
  [&quot;4&quot;, &quot;5&quot;, &quot;6&quot;]
  [&quot;7&quot;, nil, nil]

  %w(1 2 3).in_groups_of(2, '&amp;nbsp;') {|group| p group}
  [&quot;1&quot;, &quot;2&quot;]
  [&quot;3&quot;, &quot;&amp;nbsp;&quot;]

  %w(1 2 3).in_groups_of(2, false) {|group| p group}
  [&quot;1&quot;, &quot;2&quot;]
  [&quot;3&quot;]
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000492-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000492-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/grouping.rb, line 22</span>
22:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">in_groups_of</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">fill_with</span> = <span class="ruby-keyword kw">nil</span>)
23:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fill_with</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">false</span>
24:             <span class="ruby-identifier">collection</span> = <span class="ruby-keyword kw">self</span>
25:           <span class="ruby-keyword kw">else</span>
26:             <span class="ruby-comment cmt"># size % number gives how many extra we have;</span>
27:             <span class="ruby-comment cmt"># subtracting from number gives how many to add;</span>
28:             <span class="ruby-comment cmt"># modulo number ensures we don't add group of just fill.</span>
29:             <span class="ruby-identifier">padding</span> = (<span class="ruby-identifier">number</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">size</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">number</span>) <span class="ruby-operator">%</span> <span class="ruby-identifier">number</span>
30:             <span class="ruby-identifier">collection</span> = <span class="ruby-identifier">dup</span>.<span class="ruby-identifier">concat</span>([<span class="ruby-identifier">fill_with</span>] <span class="ruby-operator">*</span> <span class="ruby-identifier">padding</span>)
31:           <span class="ruby-keyword kw">end</span>
32: 
33:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
34:             <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">each_slice</span>(<span class="ruby-identifier">number</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">slice</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">slice</span>) }
35:           <span class="ruby-keyword kw">else</span>
36:             <span class="ruby-identifier">returning</span> [] <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">groups</span><span class="ruby-operator">|</span>
37:               <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">each_slice</span>(<span class="ruby-identifier">number</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">group</span><span class="ruby-operator">|</span> <span class="ruby-identifier">groups</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">group</span> }
38:             <span class="ruby-keyword kw">end</span>
39:           <span class="ruby-keyword kw">end</span>
40:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000494" class="method-detail">
        <a name="M000494"></a>

        <div class="method-heading">
          <a href="#M000494" class="method-signature">
          <span class="method-name">split</span><span class="method-args">(value = nil) {|element| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Divides the array into one or more subarrays based on a delimiting
<tt>value</tt> or the result of an optional block.
</p>
<pre>
  [1, 2, 3, 4, 5].split(3)                # =&gt; [[1, 2], [4, 5]]
  (1..10).to_a.split { |i| i % 3 == 0 }   # =&gt; [[1, 2], [4, 5], [7, 8], [10]]
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000494-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000494-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/grouping.rb, line 90</span>
 90:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">split</span>(<span class="ruby-identifier">value</span> = <span class="ruby-keyword kw">nil</span>)
 91:           <span class="ruby-identifier">using_block</span> = <span class="ruby-identifier">block_given?</span>
 92: 
 93:           <span class="ruby-identifier">inject</span>([[]]) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">results</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
 94:             <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">using_block</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">element</span>)) <span class="ruby-operator">||</span> (<span class="ruby-identifier">value</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">element</span>)
 95:               <span class="ruby-identifier">results</span> <span class="ruby-operator">&lt;&lt;</span> []
 96:             <span class="ruby-keyword kw">else</span>
 97:               <span class="ruby-identifier">results</span>.<span class="ruby-identifier">last</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">element</span>
 98:             <span class="ruby-keyword kw">end</span>
 99: 
100:             <span class="ruby-identifier">results</span>
101:           <span class="ruby-keyword kw">end</span>
102:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>